<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/8
 * Time: 9:33
 */
namespace App\Modules\Jobs\Services;
use App\Modules\FormId\Models\Formid;

use DB;
use Illuminate\Support\Facades\Log;

class SendTemplateMessageService {
    /**
     * 提现通知
     * @param $uid
     * @param $money
     * [
        'touser' => 'user-openid',
        'template_id' => 'template-id',
        'page' => 'index',
        'form_id' => 'form-id',
        'data' => [
            'keyword1' => 'VALUE',
            'keyword2' => 'VALUE2',
            // ...
        ],
     *]
     */
    public static function sendWithdrawMsg($uid,$money,$page) {
        $templateId = 'Pn2vKNvrZwtu7HWVs9h9_tpJBlX8Bl__Qf43qKKygAA';
        $user = DB::table('users')
            ->select('openid')
            ->where('id','=',$uid)
            ->first();
        $openid = $user->openid;
        if(!$openid) {
            throw new \Exception('User not find','10010');
        }
        $formIdModel = Formid::getFormIdOne($uid);
        if(!$formIdModel) {
            throw new \Exception('formId not found for user!','10011');
        }
        $formId = $formIdModel->form_id;
        $data = array(
            'touser' => $openid,
            'template_id' => $templateId,
            'page' => $page,
            'form_id' => $formId,
            'data' => [
                'keyword1' => $money,
                'keyword2' => $money-5,
                'keyword3' => 5,
                'keyword4' => date('m月d日 H:i'),
                'keyword5' => '帮找-酬金提现'
            ],
        );
        JobService::sendTemplateMessage($data,$formIdModel);
    }

    /**
     * 退款通知
     * @param $uid
     * @param $money
     * @param $page
     * @throws \Exception
     */
    public static function SendRefundMsg($uid,$money,$task_id,$page) {
        try{
            $templateId = 'cwPV5ACwnfrNY1UKIffPuEuifaJLKgxl6ttQZJTrkgU';
            $user = DB::table('users')
                ->select('openid')
                ->where('id','=',$uid)
                ->first();
            $openid = $user->openid;
            $task = DB::table('tasks')->where('id','=',$task_id)->first();
            $task_title = $task->title;
            //Log::info('task_title'.$task_title);
            if(!$openid) {
                throw new \Exception('User not find','10010');
            }
            $formIdModel = Formid::getFormIdOne($uid);
            if(!$formIdModel) {
                throw new \Exception('formId not found for user!','10011');
            }
            $formId = $formIdModel->form_id;
            $data = array(
                'touser' => $openid,
                'template_id' => $templateId,
                'page' => $page,
                'form_id' => $formId,
                'data' => [
                    'keyword1' => $money,
                    'keyword2' => '帮找任务'.$task_title.'未酬金额'.$money.'元',
                    'keyword3' => '微信钱包',
                    'keyword4' => '剩余酬金进入小程序账户，点击此处查看',
                ],
            );
            JobService::sendTemplateMessage($data,$formIdModel);
            Log::info('refound job done');
        }catch (\Exception $e){
            throw new \Exception($e->getMessage(),$e->getCode());
        }

    }

    /**
     * 留言通知
     * 發給自薦者的上級轉發者
     * @param $uid
     * @param $selfRecoId
     * @throws \Exception
     */
    public static function sendMessageTemp($puid,$selfRecoId) {
        $templateId = 'GzwL4qt6t9eimvM8tj2KFIpiO9hppSF6cbUEd24P9kA';
        $selfReco = DB::table('self_recommendations')
            ->where('id','=',$selfRecoId)
            ->first();


        $user = DB::table('users')
                ->select('openid','id')
                ->where('id','=',$puid)
                ->first();

        $openid = $user->openid;
        if(!$openid) {
                throw new \Exception('User not find','10010');
        }
        $formIdModel = Formid::getFormIdOne($puid);
        if(!$formIdModel) {
            throw new \Exception('formId not found for user!','10011');
        }
        $formId = $formIdModel->form_id;

        $RecoUser = DB::table('users')
            ->select('nickname')
            ->where('id','=',$selfReco->uid)
            ->first();
        $RecoUser = $RecoUser->nickname;
        $task = DB::table('tasks')->where('id','=',$selfReco->task_id)->first();
        if($task->uid == $user->id){
            return false;
        }
        $task_title = $task->title;
        $data = array(
            'touser' => $openid,
            'template_id' => $templateId,
            'page' => 'pages/helptoself/helptoself?sid='.$selfRecoId,
            'form_id' => $formId,
            'data' => [
                    'keyword1' =>'好友'.$RecoUser.'发起自荐，快来为好友撰写引荐语~',
                    'keyword2' => $task_title,
                    'keyword3' => $selfReco->title,
            ],
        );
        JobService::sendTemplateMessage($data,$formIdModel);
        //}

    }

    public static function recoProcess($selfRecoId,$action='') {
        $templateId = 'tOH_me6WBXx_b4uhn9_OpEP2pvjxBS4wjmALIZ';
        if(in_array($action,['Adopt','Refuse','Reward'])){
            $selfReco = DB::table('self_recommendations')
                ->where('id','=',$selfRecoId)
                ->first();

            $user = DB::table('users')
                ->select('openid')
                ->where('id','=',$selfReco->uid)
                ->first();

            $openid = $user->openid;
            if(!$openid) {
                throw new \Exception('User not find','10010');
            }
            $formIdModel = Formid::getFormIdOne($user->id);
            if(!$formIdModel) {
                throw new \Exception('formId not found for user!','10011');
            }
            $keyword1 = $selfReco->title;
            switch ($action) {
                case  'Adopt':$keyword2 = '待沟通';break;
                case 'Refuse':$keyword2 = '不合适';break;
                case 'Reward':$keyword2 = '自荐成功';break;
                default:
            }
            $formId = $formIdModel->form_id;
            $data = array(
                'touser' => $openid,
                'template_id' => $templateId,
                'page' => 'pages/rmdselfdetail/rmdselfdetail?task_id='.$selfRecoId,
                'form_id' => $formId,
                'data' => [
                    'keyword1' =>$keyword1,
                    'keyword2' => $keyword2,
                    'keyword3' => '点击查看自荐详情！',
                ],
            );
            JobService::sendTemplateMessage($data,$formIdModel);
        }

    }

}